package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.dto.CheckLogDTO;
import com.woniuxy.factory.entity.CheckLog;
import com.woniuxy.factory.entity.WorkShop;
import com.woniuxy.factory.mapper.CheckLogMapper;
import com.woniuxy.factory.mapper.WorkShopMapper;
import com.woniuxy.factory.service.CheckLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CheckLogServiceImp implements CheckLogService {
    @Autowired
    CheckLogMapper checkLogMapper;

    @Autowired
    WorkShopMapper workShopMapper;

    @Override
    public void addCheckLog(CheckLog checkLog) throws Exception {
        //设置时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String data = df.format(new Date());
        Date parse = df.parse(data);
        checkLog.setCheckTime(parse);
        //检查数据库中时间是否重复
        Date d = checkLogMapper.selectDateById(parse);
        if (d != null) {
            throw new Exception("今日已添加日志，请勿重新添加，可以修改");
        }
        checkLogMapper.inserCheckLog(checkLog);
    }

    @Override
    public void modifyCheckLog(CheckLog checkLog) {
        checkLogMapper.updateCheckLog(checkLog);
    }

    @Override
    public List<CheckLogDTO> showCheckLogBCodition(CheckLog checkLog, int page, int limit) {
        int index = (page - 1) * limit;
        //查询为空返回
        List<CheckLog> checkLogs = checkLogMapper.selectCheckLogByCondition(checkLog, index, limit);
        if (checkLogs == null) {
            return null;
        }
        //二次封装数据
        List<CheckLogDTO> checkLogDTOs = new ArrayList<>();
        WorkShop workShop;
        CheckLogDTO checkLogDTO;
        for (CheckLog cl : checkLogs) {
            workShop = workShopMapper.selectWorkShopById(cl.getWosId());
            checkLogDTO = new CheckLogDTO();
            checkLogDTO.setWorkShop(workShop);
            checkLogDTO.setCheckLog(cl);
            checkLogDTOs.add(checkLogDTO);
        }
        return checkLogDTOs;
    }
}
